package com.cli.springboottemplateengine.engine.analyze;

import java.util.Set;

public class Test {
//    public static void main(String[] args) {
//        String freemarkerTpl = "<#if user.age > 18>${user.name}</#if>";
//        String thymeleafTpl = "<div th:if=\"${user.age > 18}\">Hello [(${user.name})]</div>";
//        String groovyTpl = "Hello ${user.name}, <%= if (user.age > 18) { %>Adult<% } %>";
//
//        TemplateVariableAnalyzer fmAnalyzer = TemplateVariableAnalyzerFactory.getAnalyzer(TemplateVariableAnalyzerFactory.Engine.FREEMARKER);
//        TemplateVariableAnalyzer thAnalyzer = TemplateVariableAnalyzerFactory.getAnalyzer(TemplateVariableAnalyzerFactory.Engine.THYMELEAF);
//        TemplateVariableAnalyzer groovyAnalyzer = TemplateVariableAnalyzerFactory.getAnalyzer(TemplateVariableAnalyzerFactory.Engine.GROOVY);
//
//        System.out.println("Freemarker variables: " + fmAnalyzer.extractVariables(freemarkerTpl));
//        System.out.println("Thymeleaf variables: " + thAnalyzer.extractVariables(thymeleafTpl));
//        System.out.println("Groovy variables: " + groovyAnalyzer.extractVariables(groovyTpl));
//    }


    public static void main(String[] args) throws Exception {
        String template = "你好, ${user.name}!\n" +
                "<% def welcome = '欢迎你,' %>\n" +
                "${welcome} 你的购物车里有 ${cart.items.size()} 件商品。\n" +
                "<% if (isAdmin) { %>\n" +
                "  <p>你是管理员。</p>\n" +
                "<% } %>\n" +
                "<ul>\n" +
                "<% for (item in productList) { %>\n" +
                "  <li>${item.name} 和 ${item.price}</li>\n" + // 循环内部变量
                "<% } %>\n" +
                "</ul>\n" +
                "公司地址: ${company.address.street}"; // 更深层次的访问

        // 获取 Groovy 的提取器 (现在是全路径版本)
//        TemplateVariableExtractor extractor = VariableExtractorFactory.getExtractor("groovy");
        TemplateVariableAnalyzer groovyAnalyzer = TemplateVariableAnalyzerFactory.getAnalyzer(TemplateVariableAnalyzerFactory.Engine.GROOVY);
        // 提取变量
        Set<String> variables = groovyAnalyzer.extractVariables(template);

        // 打印结果
        System.out.println("在模板中识别出的绑定变量全路径: " + variables);


        String tmp = "Hello, ${name}|${student.name}! How old are you? I am ${age}|${student.age} years old.\n" +
                "I am studying class: \n<% for (item in student.clazzList) { %>" +
                "  - ${item.name}\n" +
                "<% } %>${student.getName()} -- ${student.clazz.name}";

        // 提取变量
        Set<String> variablesTmp = groovyAnalyzer.extractVariables(tmp);

        // 打印结果
        System.out.println("在模板中识别出的绑定变量全路径: " + variablesTmp);
    }
}
